package com.lmk.system.service.Impl;

import com.lmk.basic.service.Impl.BaseServiceImpl;
import com.lmk.system.domain.Role;
import com.lmk.system.mapper.RoleMapper;
import com.lmk.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override//将角色添加到数据库
    @Transactional
    public void add(Role role) {
        super.add(role);
        // 添加当前角色的权限
        roleMapper.addRolePermission(role);
        // 添加当前角色的菜单
        roleMapper.addRoleMenu(role);
    }

    @Override// 修改角色
    @Transactional
    public void update(Role role) {
        super.update(role);
        // 删除角色菜单中间表信息
        roleMapper.delMenuByRoleId(role.getId());
        // 删除角色权限中间表信息
        roleMapper.delPermissionByRoleId(role.getId());
        // 添加当前角色的权限
        roleMapper.addRolePermission(role);
        // 添加当前角色的菜单
        roleMapper.addRoleMenu(role);
    }

    @Override
    @Transactional
    public void del(Long id) {

        //删除角色菜单中间表
        roleMapper.delMenuByRoleId(id);
        //删除角色权限中间表
        roleMapper.delPermissionByRoleId(id);
        //删除员工角色中间表
        roleMapper.delEmployeeByRoleId(id);
        super.del(id);

    }
}
